package cn.iocoder.yudao.module.cmt.controller.admin.content;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.content.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.content.ContentCommentDO;
import cn.iocoder.yudao.module.cmt.service.content.ContentCommentService;

@Tag(name = "管理后台 - 评论")
@RestController
@RequestMapping("/cmt/content-comment")
@Validated
public class ContentCommentController {

    @Resource
    private ContentCommentService commentService;

    @PostMapping("/create")
    @Operation(summary = "创建评论")
    @PreAuthorize("@ss.hasPermission('cmt:content-comment:create')")
    public CommonResult<Long> createComment(@Valid @RequestBody ContentCommentSaveReqVO createReqVO) {
        return success(commentService.createComment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新评论")
    @PreAuthorize("@ss.hasPermission('cmt:content-comment:update')")
    public CommonResult<Boolean> updateComment(@Valid @RequestBody ContentCommentSaveReqVO updateReqVO) {
        commentService.updateComment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除评论")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:content-comment:delete')")
    public CommonResult<Boolean> deleteComment(@RequestParam("id") Long id) {
        commentService.deleteComment(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除评论")
    @PreAuthorize("@ss.hasPermission('cmt:content-comment:delete')")
    public CommonResult<Boolean> deleteCommentList(@RequestParam("ids") List<Long> ids) {
        commentService.deleteCommentListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得评论")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:content-comment:query')")
    public CommonResult<ContentCommentRespVO> getComment(@RequestParam("id") Long id) {
        ContentCommentDO comment = commentService.getComment(id);
        return success(BeanUtils.toBean(comment, ContentCommentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得评论分页")
    @PreAuthorize("@ss.hasPermission('cmt:content-comment:query')")
    public CommonResult<PageResult<ContentCommentRespVO>> getCommentPage(@Valid ContentCommentPageReqVO pageReqVO) {
        PageResult<ContentCommentDO> pageResult = commentService.getCommentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContentCommentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出评论 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:content-comment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCommentExcel(@Valid ContentCommentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContentCommentDO> list = commentService.getCommentPage(pageReqVO).getList();
        ExcelUtils.write(response, "评论.xls", "数据", ContentCommentRespVO.class,
                        BeanUtils.toBean(list, ContentCommentRespVO.class));
    }

}
